// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "MotionSubtitlesCanvas.h"
#include "TextInfos.h"
#include "Subsystems/WorldSubsystem.h"
#include "MotionSubtitlesSubsystem.generated.h"

/**
 * 
 */
UCLASS()
class MOTIONSUBTITLES_API UMotionSubtitlesSubsystem : public UWorldSubsystem
{
	GENERATED_BODY()

private:
	TMap<FGuid, TWeakObjectPtr<AActor>> Mappings;

	TWeakObjectPtr<AMotionSubtitlesCanvas> Canvas;

public:
	// 获取单例
	static UMotionSubtitlesSubsystem* Get();

	// 生命周期
	virtual bool ShouldCreateSubsystem(UObject* Outer) const override;

	virtual void Initialize(FSubsystemCollectionBase& Collection) override;

	virtual void Deinitialize() override;

public:
	bool ShowOnce(const UObject* WorldContextObject, const FString& InFolderPath, const FSubtitleParams& Params,
	              const TFunction<void()>& OnFinished);

protected:
	// 创建Subtitles
	bool Create(const UObject* WorldContextObject, const FString& InFolderPath, FGuid& Id);

	// 显示文字
	bool Show(const UObject* WorldContextObject, const FGuid& Id, const FSubtitleParams& Params,
	          const TFunction<void()>& OnFinished);

	// 销毁Subtitles
	bool Destroy(const FGuid Id);

	UFUNCTION(BlueprintCallable)
	bool Register(AActor* Actor, FGuid& Id);

	UFUNCTION(BlueprintCallable)
	bool UnRegister(const FGuid Id, AActor*& Actor);

private:
	// Pak已挂载的情况下，获取Pak内所有uasset
	bool GetAllFiles(const FString& InFolderPath, TArray<FString>& Files);

	// 拼接资源名称，用于资源加载
	bool GetAssetName(const FString& FilePath, FString& AssetName);

	// 创建MotionSubtitlesActor
	bool CreateSubtitlesActor(const UObject* WorldContextObject, const FString& AssetName, AActor*& SubtitlesActor);

	// 检测Subtitles
	bool IsValidSubtitles(const FGuid& Id);

	bool IsValidSubtitles(const AActor* Actor);
};
